package world.xuewei.service.impl;

import world.xuewei.model.entity.Address;
import world.xuewei.dao.AddressDao;
import world.xuewei.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收货地址表(Address)表服务实现类
 *
 * @author makejava
 * @since 2024-11-15 19:27:47
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressDao addressDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Address queryById(Long id) {
        return this.addressDao.queryById(id);
    }

    /**
     * 查询当前用户的地址信息
     *
     * @param userId     用户ID
     * @return 查询结果
     */
    @Override
    public List<Address> queryByUserAddress(Long userId) {
        return this.addressDao.queryByUserAddress(userId);
    }

    /**
     * 新增数据
     *
     * @param address 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Address address) {
         int i = this.addressDao.insert(address);
        // 如果是默认地址则修改默认地址
        if (address.getIsDefault() == 1) {
            this.addressDao.setDefault(address.getId(), address.getUserId());
        }
        return i;
    }

    /**
     * 修改数据
     *
     * @param address 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Address address) {
        return this.addressDao.update(address);
    }

    /**
     * 修改默认地址
     *
     * @param id 收货地址id
     * @param userId 用户id
     * @return 影响行数
     */
    @Override
    public int setDefault(Long id, Long userId) {
        return this.addressDao.setDefault(id, userId);
    }
    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Long id, Long userId) {
        return this.addressDao.deleteById(id, userId);
    }
}
